package com.dayfly.tip.action;

import com.dayfly.tip.ao.UserAO;
import com.dayfly.tip.model.Result;
import com.dayfly.tip.model.UserDO;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author dayfly
 * 
 */
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 938148981519522165L;

	private UserAO userAO;

	public void setUserAO(UserAO userAO) {
		this.userAO = userAO;
	}

	@Override
	public String execute() throws Exception {
		if (ActionContext.getContext().getSession().get("userId") != null) {
			return SUCCESS;
		}
		if (getUserName() != null && getPassword() != null) {
			Result result = userAO.getUser(getUserName(), getPassword());
			if (result.isSuccess()) {
				UserDO user = (UserDO) result.get("user");
				if (user == null) {
					return ERROR;
				} else {
					ActionContext.getContext().getSession()
							.put("userId", user.getUserId());
					ActionContext.getContext().getSession()
							.put("userNick", user.getUserName());
					return SUCCESS;
				}
			}
		}

		return INPUT;
	}

	public String check() throws Exception {
		if ("feifei".equals(getUserName()) && "123".equals(getPassword())) {
			ActionContext.getContext().getSession().put("userId", 12345L);
			ActionContext.getContext().getSession().put("userNick", "feifei");
			return SUCCESS;
		} else {
			if (ActionContext.getContext().getSession().get("userId") != null) {
				return SUCCESS;
			}
			return INPUT;
		}

	}

}
